const adminService = require('service/admin');

function* signIn(next) {
	let { username, password } = this.request.body;
	let admin = yield adminService.signIn({ username, password });
	let token = this.sign(admin.id);
	yield this.setSession(admin, token);
	admin.token = token;
	delete admin.password;

	//菜单权限 暂用于测试
	let roles = [{
		menu: '/user'
	}, {
		menu: '/category'
	}]

	admin.roles = roles;

	this.body = this.success(admin);
	return yield next;
};

function* getAdmins(next) {
	let { page, limit } = this.query;
	let admins = yield adminService.getAdmins(page, limit);
	this.body = this.success(admins);
	return yield next;
}

function* addAdmin(next) {
	let { username, password } = this.request.body;
	yield adminService.addAdmin(username, password);
	this.body = this.success();
	return yield next;
}

function* deleteAdmin(next) {
	let { id } = this.params;
	let ids = [];
	ids.push(id);
	yield adminService.deleteAdmins(ids);
	this.body = this.success();
	return yield next;
}

function* updateAdminPassword(next) {
	let { id } = this.params;
	let { password } = this.request.body;
	yield adminService.updateAdminPassword(id, password);
	this.body = this.success();
	return yield next;
}

module.exports = {
	signIn,
	getAdmins,
	addAdmin,
	deleteAdmin,
	updateAdminPassword
}